<template>
  <section class="msite">
    <!--首页头部使用插槽-->
    <HeadTop :title="address.name" >
      <span class="header_search" slot="left">
        <i class="iconfont icon-sousuo"></i>
      </span>
      <span class="header_login" slot="right">
        <span class="header_login_text">登录|注册</span>
      </span>
    </HeadTop>
    <!--首页导航-->
    <nav class="msite_nav">
      <div class="swiper-container" v-if="categorysArr.length>0">
        <div class="swiper-wrapper">
          <div class="swiper-slide" v-for="(cs, index) in categorysArr" :key="index">
            <a href="javascript:" class="link_to_food" v-for="(c, index2) in cs" :key="index2">
              <div class="food_container">
                <img :src="imgBaseUrl+c.image_url">
              </div>
              <span>{{c.title}}</span>
            </a>
          </div>
        </div>
        <!-- Add Pagination -->
        <div class="swiper-pagination"></div>
      </div>
      <img src="./images/msite_back.svg" v-else>
    </nav>
    <!--首页附近商家-->
    <ShopList />
  </section>
</template>

<script>
/* eslint-disable no-new */
// 导入插槽组件
import HeadTop from '../../common/HeadTop/HeadTop'
// 导入 Swiper 轮播对象
import Swiper from 'swiper'
// 引入轮播样式
import 'swiper/dist/css/swiper.min.css'

// 导入 ShopList 组件
import ShopList from '../../components/ShopList/ShopList.vue'

// 引入  mapGetters
import { mapState } from 'vuex'

export default {
  name: 'MSite',
  components: {
    HeadTop,
    ShopList
  },
  mounted () {
    // 请求获得所有的商品类别对象
    this.$store.dispatch('getCategorys')
  },
  data () {
    return {
      // address: {name: 'bart'}
      imgBaseUrl: 'https://fuss10.elemecdn.com'
    }
  },
  computed: {
    ...mapState(['address', 'categorys']), // 映射 store 的属性的 getter
    categorysArr () {
      console.log('================> MSite.vue -> this.$store.state.categorys : ', this.$store.state.categorys)
      let arr = []
      let minArr = []
      // let { categorys } = this
      let categorys = this.$store.state.categorys
      categorys.forEach((value, index) => {
        if (minArr.length === 8) {
          minArr = []
        }
        if (minArr.length === 0) {
          arr.push(minArr)
        }
        minArr.push(value)
      })
      return arr
    }
  },
  watch: { // 监听
    categorys () {
      // 在页面DOM视图渲染完毕后在执行
      this.$nextTick(() => {
        new Swiper('.swiper-container', {
          pagination: {
            el: '.swiper-pagination'
          },
          loop: true
        })

        // new BScroll('.miste-content-wrapper', {
        //   click: true
        // })
      })
    }
  }
}
</script>

<style lang="stylus" ref="stylesheet/stylus">
  //引入外部的stylus样式
  @import "../../common/stylus/mixins.styl"
  .msite  //首页
    width 100%
    .msite_nav
      bottom-border-1px(#e4e4e4)
      margin-top 45px
      height 200px
      background #fff
      .swiper-container
        width 100%
        height 100%
        .swiper-wrapper
          width 100%
          height 100%
          .swiper-slide
            display flex
            justify-content center
            align-items flex-start
            flex-wrap wrap
            .link_to_food
              width 25%
              .food_container
                display block
                width 100%
                text-align center
                padding-bottom 10px
                font-size 0
                img
                  display inline-block
                  width 50px
                  height 50px
              span
                display block
                width 100%
                text-align center
                font-size 13px
                color #666
        .swiper-pagination
          >span.swiper-pagination-bullet-active
            background #02a774
</style>
